package observer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
 * 具体的被观察者
 * @Description: 
 * @author wanglong
 * @createDate: 2019年3月17日 下午3:05:15
 */
public class WeatherData implements Subject{
	
	
	private List<Observer> observers;
	private float temperature;
	private float humidity;
	private float pressure;
	
	public WeatherData() {
		observers = new ArrayList<>();
	}
	
	public void setMeasurements(float temperature, float humidity, float pressure) {
		this.temperature = temperature;
		this.humidity = humidity;
		this.pressure = pressure;
		notifyObserver();
	}

	@Override
	public void registerObserver(Observer o) {
		observers.add(o);
		
	}

	@Override
	public void removeObserver(Observer o) {
		/*int i = observers.indexOf(o);
		if(i >= 0) {
			observers.remove(i);
		}*/
		Iterator<Observer> iterator = observers.iterator();
		while(iterator.hasNext()) {
			if(iterator.next().equals(o)) {
				iterator.remove();
			}
		}
	}

	@Override
	public void notifyObserver() {
		observers.forEach(o -> {
			o.update(temperature, humidity, pressure);
		});
		
	}

}
